using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Crm2011.Framework.Core;
using Microsoft.Xrm.Sdk.Client;



namespace Crm2011.Framework.Connect
{
    /// <summary>
    /// The CrmConnectionEditor edits a named CRM ConnectionProfile.
    /// </summary>
    public partial class CrmConnectionEditor : Form
    {
        public CrmConnectionEditor()
        {
            InitializeComponent();
            comboConnectionType.Items.Add(new ComboItem(AuthenticationProviderType.ActiveDirectory, "ActiveDirectory"));
            comboConnectionType.Items.Add(new ComboItem(AuthenticationProviderType.LiveId, "LiveId / Office 365"));
            comboConnectionType.DisplayMember ="Text";
            comboConnectionType.ValueMember ="Value";
            comboConnectionType.SelectedIndex = 1;
        }




        class ComboItem
        {
            public ComboItem(AuthenticationProviderType value, string text)
            {
                Value = value;
                Text = text;
            }


            public AuthenticationProviderType Value { get; private set; }
            public string Text { get; private set; }   
        }



        /// <summary>
        /// Gets or sets the selected authentication.
        /// </summary>
        private ComboItem SelectedAuthentication
        {
            get { return (ComboItem)comboConnectionType.SelectedItem; }
            set { comboConnectionType.SelectedItem = value; }
        }



        /// <summary>
        /// Gets or sets the CRM ConnectionProfile string.
        /// </summary>
        /// <value>
        /// The CRM ConnectionProfile string.
        /// </value>
        public ConnectionProfile GetConnectionProfile()
        {
            _connectionProfile.ConnectionString = getActiveConnectionControl().GetCrmConnectionString().GetConnectionStringSecure();
            _connectionProfile.ConnectionName = this.textProfileName.Text;
            return _connectionProfile;
        }



        public void SetConnectionProfile(ConnectionProfile value)
        {
            if (value == null)
                throw new InvalidOperationException("Cannot edit null profile!");

            _connectionProfile = value;
            this.textProfileName.Text = _connectionProfile.ConnectionName;
            CrmConnectionString cns = _connectionProfile.GetCrmConnectionString();

            AuthenticationProviderType newValue = (cns == null) ? AuthenticationProviderType.ActiveDirectory : cns.AuthenticationType;
            this.SelectedAuthentication = comboConnectionType.Items.Cast<ComboItem>().FirstOrDefault(item => item.Value == newValue);
            getActiveConnectionControl().SetCrmConnectionString(cns);
        }



        private ConnectionProfile _connectionProfile;



        /// <summary>
        /// The ConnectionProfile type changed.
        /// </summary>
        //private void connectionTypeChanged(object sender, EventArgs e)
        private void comboConnectionTypeSelectionChanged(object sender, EventArgs e)
        {
            this.CrmConnectionStringControl1.Visible = (this.SelectedAuthentication.Value == AuthenticationProviderType.ActiveDirectory);
            this.CrmConnectionStringControl2.Visible = (this.SelectedAuthentication.Value == AuthenticationProviderType.LiveId);
        }



        private ICrmConnectionControl getActiveConnectionControl()
        {
            if (this.SelectedAuthentication.Value == AuthenticationProviderType.ActiveDirectory) return this.CrmConnectionStringControl1;
            if (this.SelectedAuthentication.Value == AuthenticationProviderType.LiveId) return this.CrmConnectionStringControl2;
            throw new InvalidOperationException("Invalid authentication provider!");
        }
    }
}